package com.crack.nga2.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.crack.nga2.entity.Floor;
import com.crack.nga2.entity.Reply;
import com.crack.nga2.entity.SubForum;
import com.crack.nga2.entity.Theme;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库建立
 * Created by Administrator on 2016/1/19.
 */
public class DBHelper extends SQLiteOpenHelper {
    public static final String DB_FORUM = "forum_collect";
    public static final String DB_THEME = "theme_collect";
    public static final String DB_REPLY = "reply_collect";

    public DBHelper(Context context) {
        super(context, "nga.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql1 =
                "create table if not exists forum_collect(_id integer primary key autoincrement,forum_id int not null," +
                        "forum_name text not null,forum_info text)";
        String sql2 =
                "create table if not exists theme_collect(_id integer primary key autoincrement,theme_id int not null," +
                        "forum_id int not null,subject text not null,author text not null,time long not null)";
        String sql3 =
                "create table if not exists reply_collect(_id integer primary key autoincrement,reply_id int not null," +
                        "theme_id int not null,reply_content text,reply_author text not null)";
        db.execSQL(sql1);
        db.execSQL(sql2);
        db.execSQL(sql3);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean isExists(int id, String table) {
        SQLiteDatabase db = getWritableDatabase();
        String where = null;
        switch (table) {
            case DB_FORUM:
                where = "forum_id=?";
                break;
            case DB_THEME:
                where = "theme_id=?";
                break;
            case DB_REPLY:
                where = "reply_id=?";
                break;
        }
        if (where == null) {
            return false;
        }
        Cursor c = db.query(table, null, where, new String[]{String.valueOf(id)}, null, null, null);
        boolean b = c.moveToNext();
        Log.i("isCollect", "" + b);
        c.close();
        db.close();
        return b;
    }

    public int add(Object object) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        int result = 0;
        if (object instanceof SubForum) {
            SubForum forum = (SubForum) object;
            ContentValues values = new ContentValues();
            values.put("forum_id", forum.getFid());
            values.put("forum_name", forum.getName());
            values.put("forum_info", forum.getInfo());
            result = (int) db.insert(DB_FORUM, null, values);
            Log.i("add","id"+result);
        }
        if (object instanceof Theme) {
            Theme theme = (Theme) object;
            db.delete(DB_THEME, "theme_id=?", new String[]{String.valueOf(theme.getTid())});
            ContentValues values = new ContentValues();
            values.put("theme_id", theme.getTid());
            values.put("forum_id", theme.getFid());
            values.put("subject", theme.getSubject());
            values.put("author", theme.getAuthor());
            values.put("time", System.currentTimeMillis());
            result = (int) db.insert(DB_THEME, null, values);
        }
        if (object instanceof Floor) {
            Floor reply = (Floor) object;
            db.delete(DB_REPLY, "reply_id=?", new String[]{String.valueOf(reply.getPid())});
            ContentValues values = new ContentValues();
            values.put("reply_id", reply.getPid());
            values.put("theme_id", reply.getTid());
            values.put("reply_content", reply.getContent());
            values.put("reply_author", reply.getUsername());
            result = (int) db.insert(DB_REPLY, null, values);
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        return result;
    }

    public int delete(Object object) {
        SQLiteDatabase db = getWritableDatabase();
        int result = 0;
        if (object instanceof SubForum) {
            SubForum forum = (SubForum) object;
            result = db.delete(DB_FORUM, "forum_id=?", new String[]{String.valueOf(forum.getFid())});
        }
        if (object instanceof Theme) {
            Theme theme = (Theme) object;
            result = db.delete(DB_THEME, "theme_id=?", new String[]{String.valueOf(theme.getTid())});
        }
        if (object instanceof Floor) {
            Floor reply = (Floor) object;
            result = db.delete(DB_REPLY, "reply_id=?", new String[]{String.valueOf(reply.getPid())});
        }
        db.close();
        return result;
    }

    public List<SubForum> queryForumAll() {
        SQLiteDatabase db = getWritableDatabase();
        Cursor c = db.query(DB_FORUM, new String[]{"forum_id,forum_name,forum_info"}, null, null, null, null, null);
        List<SubForum> list = new ArrayList<>();
        if (c != null) {
            while (c.moveToNext()) {
                int fid = c.getInt(0);
                String name = c.getString(1);
                String info = c.getString(2);
                SubForum forum = new SubForum();
                forum.setFid(fid);
                forum.setName(name);
                forum.setInfo(info);
                list.add(forum);
            }
            c.close();
        }
        db.close();
        return list;
    }
}
